const posting = require("../models/posting")
const replyPosting = require("../models/replyPosting")
const like = require("../models/like")

var express = require('express');
var router = express.Router();

const formidable = require('formidable');

//返回图片
router.post('/', (req, res, next) => {
    var form = new formidable.IncomingForm();
    form.parse(req, async function (err, fields, files) {
        if (err) {
            res.json({ 'state': false })
        } else {
            try {
                let he = await like.find({
                    UUID: fields.UUID
                })
                if(fields.postOrNot){
                    let posting_list = [];
                    const updateLikeCount = async function(num){
                        let doc = await posting.findById(fields.posting_id, {
                            likeCount: 1,
                            _id: 0
                        }) 
                        let like_count = doc.likeCount + num;
                        let result = await posting.updateOne({
                            _id: fields.posting_id
                        }, {
                            likeCount: like_count
                        })
                        
                        if(result.ok===1){
                            let flag = false;
                            if(num === 1) flag = true;
                            res.json({
                                'state': true,
                                'likeCount': like_count,
                                'flag': flag
                            })
                            return
                        }
                        res.json({
                            'state': false
                        })
                    }
                    posting_list.push(fields.posting_id)
                    if(he.length === 0){
                        await like.create({
                            UUID: fields.UUID,
                            posting_list: posting_list
                        })
                        //点赞数加一
                        let it = await updateLikeCount(1);
                    } else {
                        // console.log(he[0].posting_list.includes(fields.posting_id))
                        if(he[0].posting_list.includes(fields.posting_id)){
                            await like.updateOne({
                                UUID:fields.UUID
                            }, {
                                $pull: {
                                    posting_list: fields.posting_id
                                }
                            })
                            //点赞数减一
                            let it = await updateLikeCount(-1);
                        } else {
                            await like.updateOne({
                                UUID: fields.UUID
                            }, {
                                $push: {
                                    posting_list: fields.posting_id
                                }
                            })
                            //点赞数加一
                            let it = await updateLikeCount(1);
                        }
                        
                    }
                } else {
                    console.log(fields.postOrNot,fields.reply_id)
                    let reply_list = [];
                    const updateLikeCount = async function(num){
                        let doc = await replyPosting.findById(fields.reply_id, {
                            likeCount: 1,
                            _id: 0
                        }) 
                        let like_count = doc.likeCount + num;
                        let result = await replyPosting.updateOne({
                            _id: fields.reply_id
                        }, {
                            likeCount: like_count
                        })
                        
                        if(result.ok===1){
                            let flag = false;
                            if(num === 1) flag = true;
                            res.json({
                                'state': true,
                                'likeCount': like_count,
                                'flag': flag
                            })
                            return
                        }
                        res.json({
                            'state': false
                        })
                    }
                    reply_list.push(fields.reply_id)
                    if(he.length === 0){
                        await like.create({
                            UUID: fields.UUID,
                            reply_list: reply_list
                        })
                        //点赞数加一
                        let it = await updateLikeCount(1);
                    } else {
                        if(he[0].reply_list.includes(fields.reply_id)){
                            await like.updateOne({
                                UUID:fields.UUID
                            }, {
                                $pull: {
                                    reply_list: fields.reply_id
                                }
                            })
                            //点赞数减一
                            let it = await updateLikeCount(-1);
                        } else {
                            await like.updateOne({
                                UUID: fields.UUID
                            }, {
                                $push: {
                                    reply_list: fields.reply_id
                                }
                            })
                            //点赞数加一
                            let it = await updateLikeCount(1);
                        }
                        
                    }
                }
                
            } catch(err){
                // console.log(err)
                res.json({
                    'state': false,
                    'msg': err
                })
            }
            
        }

    });

});

module.exports = router;